# -*- coding: utf-8 -*-

import os

import numpy as np
import pandas as pd
import string

from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Pie
from pyecharts.globals import ThemeType

from pyecharts.render import make_snapshot
# pip install snapshot-selenium
from snapshot_selenium import snapshot

from py_pyecharts import py_dir_path


# pyecharts支持链式调用


def line_chart():
    """折线图"""
    x_data = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    y_data = [820, 932, 901, 934, 1290, 1330, 1320]
    line = (
        Line(
            init_opts=opts.InitOpts()
        ).add_xaxis(
            xaxis_data=x_data
        ).add_yaxis(
            series_name='',
            y_axis=y_data,
            symbol='emptyCircle',
            is_symbol_show=True,
            label_opts=opts.LabelOpts(is_show=False),
            areastyle_opts=opts.AreaStyleOpts(opacity=1, color='#C67570'),
        ).set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(
                type_='value',
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(
                type_='category',
                boundary_gap=False),
        )
    )
    line.render(path=os.path.join(py_dir_path, 'line_chart.html'))
    pass


def bar_chart():
    """柱状图"""
    bar = (
        Bar(
            init_opts=opts.InitOpts(theme=ThemeType.DARK)
        ).add_xaxis(
            xaxis_data=['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        ).add_yaxis(
            series_name='商家A',
            y_axis=[5, 20, 36, 10, 75, 90]
        ).add_yaxis(
            series_name='商家B',
            y_axis=[15, 6, 45, 20, 35, 66]
        ).set_global_opts(
            title_opts=opts.TitleOpts(title='主标题', subtitle='副标题')
        )
    )
    bar.render(path=os.path.join(py_dir_path, 'bar_chart.html'))
    pass


def pie_chart():
    rows = 4
    cols = 1
    unit = 0.1
    df = pd.DataFrame(data=np.arange(1 * unit, rows * cols * unit, 1 * unit).reshape(rows, cols),
                      index=list(string.ascii_lowercase[:rows]),
                      columns=list(string.ascii_uppercase[:cols]))
    df = df.applymap(lambda x: round(x, 4))
    pie = (
        Pie(
            init_opts=opts.InitOpts(width='600px', height='400px')
        ).add(
            series_name='series[A]',
            data_pair=[list(i) for i in zip(df.index, df['A'])],
            # 圆心位置left, top
            center=['center', 'middle'],
            # 饼图pie:radius=半径, 圆环图doughnut:radius=[内径，外径]
            radius='80%'
        ).set_series_opts(
            label_opts=opts.LabelOpts(formatter='{b}\n{c}%'),
            # 边界宽度和颜色设置
            itemstyle_opts=opts.ItemStyleOpts(border_width=1, border_color='#ffffff')
        ).set_global_opts(
            title_opts=opts.TitleOpts(title='title'),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )
    pie.render(path=os.path.join(py_dir_path, 'pie_chart.html'))
    pass


if __name__ == '__main__':
    # line_chart()
    bar_chart()
    # pie_chart()

    pass
